<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>在数组中搜索给定的值，如果成功则返回相应的键名</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="function.array-reverse.html">array_reverse</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="function.array-shift.html">array_shift</a></div>
 <div class="up"><a href="ref.array.html">数组 函数</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="function.array-search" class="refentry">
   <div class="refnamediv">
    <h1 class="refname">array_search</h1>
    <p class="verinfo">(PHP 4 &gt;= 4.0.5, PHP 5)</p><p class="refpurpose"><span class="refname">array_search</span> &mdash; <span class="dc-title">
     在数组中搜索给定的值，如果成功则返回相应的键名
    </span></p>

   </div>
 <div class="refsect1 description" id="refsect1-function.array-search-description">
  <h3 class="title">说明</h3>
     <div class="methodsynopsis dc-description">
      <span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <span class="methodname"><strong>array_search</strong></span>
       ( <span class="methodparam"><span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <code class="parameter">$needle</code></span>
      , <span class="methodparam"><span class="type">array</span> <code class="parameter">$haystack</code></span>
   [, <span class="methodparam"><span class="type">bool</span> <code class="parameter">$strict</code><span class="initializer"> = false</span></span>
     ] )</div>

    <p class="para rdfs-comment">
     在 <em><code class="parameter">haystack</code></em> 中搜索
     <em><code class="parameter">needle</code></em> 参数。
    </p>
 </div>

 <div class="refsect1 parameters" id="refsect1-function.array-search-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>

    
     <dt>
<em><code class="parameter">needle</code></em></dt>

     <dd>

      <p class="para">
       搜索的值。
      </p>
      <blockquote class="note"><p><strong class="note">Note</strong>: 
       <p class="para">
        如果 <em><code class="parameter">needle</code></em> 是字符串，则比较以区分大小写的方式进行。
       </p>
      </p></blockquote>
     </dd>

    
    
     <dt>
<em><code class="parameter">haystack</code></em></dt>

     <dd>

      <p class="para">
       这个数组。
      </p>
     </dd>

    
    
     <dt>
<em><code class="parameter">strict</code></em></dt>

     <dd>

      <p class="para">
       如果可选的第三个参数 <em><code class="parameter">strict</code></em> 为 <strong><code>TRUE</code></strong>，则
     <span class="function"><strong>array_search()</strong></span> 将在 <em><code class="parameter">haystack</code></em>
     中检查<em class="emphasis">完全相同</em>的元素。
       这意味着同样检查 <em><code class="parameter">haystack</code></em> 里 <em><code class="parameter">needle</code></em> 的 <a href="language.types.html" class="link">类型</a>，并且对象需是同一个实例。
      </p>
     </dd>

    
   </dl>

  </p>
 </div>

 <div class="refsect1 returnvalues" id="refsect1-function.array-search-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   如果找到了 <em><code class="parameter">needle</code></em> 则返回它的键，否则返回 <strong><code>FALSE</code></strong>。
  </p>
  <p class="para">
   如果 <em><code class="parameter">needle</code></em> 在
     <em><code class="parameter">haystack</code></em>
     中出现不止一次，则返回第一个匹配的键。要返回所有匹配值的键，应该用
     <span class="function"><a href="function.array-keys.html" class="function">array_keys()</a></span> 加上可选参数
     <em><code class="parameter">search_value</code></em> 来代替。
  </p>
  <div class="warning"><strong class="warning">Warning</strong><p class="simpara">此函数可能返回布尔值
<strong><code>FALSE</code></strong>，但也可能返回等同于 <strong><code>FALSE</code></strong> 的非布尔值。请阅读 <a href="language.types.boolean.html" class="link">布尔类型</a>章节以获取更多信息。应使用
<a href="language.operators.comparison.html" class="link">===
运算符</a>来测试此函数的返回值。</p></div>
 </div>

 <div class="refsect1 changelog" id="refsect1-function.array-search-changelog">
  <h3 class="title">更新日志</h3>
  <p class="para">
   <table class="doctable informaltable">
    
     <thead>
      <tr>
       <th>版本</th>
       <th>说明</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr>
       <td>5.3.0</td>
       <td>
        As with all internal PHP functions as of 5.3.0,
        <span class="function"><strong>array_search()</strong></span> returns <strong><code>NULL</code></strong> if invalid parameters
        are passed to it.
       </td>
      </tr>

      <tr>
       <td>4.2.0</td>
       <td>
        在 PHP 4.2.0 之前，<span class="function"><strong>array_search()</strong></span> 在失败时返回
      <strong><code>NULL</code></strong> 而不是 <strong><code>FALSE</code></strong>。
       </td>
      </tr>

     </tbody>
    
   </table>

  </p>
 </div>

  <div class="refsect1 examples" id="refsect1-function.array-search-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-4882">
    <p><strong>Example #1 <span class="function"><strong>array_search()</strong></span> 例子</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$array&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #0000BB">0&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'blue'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">1&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'red'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">2&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'green'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">3&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'red'</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$key&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">array_search</span><span style="color: #007700">(</span><span style="color: #DD0000">'green'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$array</span><span style="color: #007700">);&nbsp;</span><span style="color: #FF8000">//&nbsp;$key&nbsp;=&nbsp;2;<br /></span><span style="color: #0000BB">$key&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">array_search</span><span style="color: #007700">(</span><span style="color: #DD0000">'red'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$array</span><span style="color: #007700">);&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;$key&nbsp;=&nbsp;1;<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </p>
 </div>

 <div class="refsect1 seealso" id="refsect1-function.array-search-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="function.array-keys.html" class="function" rel="rdfs-seeAlso">array_keys()</a> - 返回数组中所有的键名</span></li>
    <li class="member"><span class="function"><a href="function.array-values.html" class="function" rel="rdfs-seeAlso">array_values()</a> - 返回数组中所有的值</span></li>
    <li class="member"><span class="function"><a href="function.array-key-exists.html" class="function" rel="rdfs-seeAlso">array_key_exists()</a> - 检查给定的键名或索引是否存在于数组中</span></li>
    <li class="member"><span class="function"><a href="function.in-array.html" class="function" rel="rdfs-seeAlso">in_array()</a> - 检查数组中是否存在某个值</span></li>
   </ul>
  </p>
 </div>

  </div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="function.array-reverse.html">array_reverse</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="function.array-shift.html">array_shift</a></div>
 <div class="up"><a href="ref.array.html">数组 函数</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>
